package com.ph.irp.base.measure.action;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.measure.service.PBaseMeasureUnitService;
import com.ph.irp.base.measureunitgroup.service.PBaseMeasureUnitGroupService;
import com.ph.irp.dto.MeasureDTO;
import com.ph.irp.model.PBaseMeasuregroup;
import com.ph.irp.model.PBaseMeasureunit;
import com.ph.util.MathUtil;

public class MeasureUnitAction extends ActionSupport {
	protected final static Format TWO = new DecimalFormat("0.00");
	private String FORWARD = null;

	private PBaseMeasureUnitService measureUnitService;
	private PBaseMeasureUnitGroupService measureGroupService;
	private List<MeasureDTO> measureDTOList = null;
	private PBaseMeasureunit measureunit = null;
	private Object[] rowlist = null;
	private PBaseMeasuregroup measureunitgroup = null;
	private String successString;
	private String rowIndex;

	public String getRowIndex() {
		return rowIndex;
	}

	public void setRowIndex(String rowIndex) {
		this.rowIndex = rowIndex;
	}

	public PBaseMeasureUnitService getMeasureUnitService() {
		return measureUnitService;
	}

	public void setMeasureUnitService(PBaseMeasureUnitService measureUnitService) {
		this.measureUnitService = measureUnitService;
	}

	public PBaseMeasureUnitGroupService getMeasureGroupService() {
		return measureGroupService;
	}

	public void setMeasureGroupService(
			PBaseMeasureUnitGroupService measureGroupService) {
		this.measureGroupService = measureGroupService;
	}

	public List<MeasureDTO> getMeasureDTOList() {
		return measureDTOList;
	}

	public void setMeasureDTOList(List<MeasureDTO> measureDTOList) {
		this.measureDTOList = measureDTOList;
	}

	public PBaseMeasureunit getMeasureunit() {
		return measureunit;
	}

	public void setMeasureunit(PBaseMeasureunit measureunit) {
		this.measureunit = measureunit;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public PBaseMeasuregroup getMeasureunitgroup() {
		return measureunitgroup;
	}

	public void setMeasureunitgroup(PBaseMeasuregroup measureunitgroup) {
		this.measureunitgroup = measureunitgroup;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	/**
	 * 
	 * 
	 * 
	 */
	private static final long serialVersionUID = -4132219473301108793L;

	public String addMeasure() throws Throwable {

		measureDTOList = this.measureUnitService.getMeasureUnitAll();
		if (measureunit.getIsdefault() == 1) {
			for (int i = 0; i < measureDTOList.size(); i++) {

				if (measureDTOList.get(i).getMeasureunit().getMeasureGroupId() == measureunit
						.getMeasureGroupId()
						&& measureDTOList.get(i).getMeasureunit()
								.getIsdefault() == 1
						&& measureunit.getIsdefault() == 1) {

					setSuccessString("同一个计量单位组中只能有一个默认单位");
					FORWARD = "input";
					return FORWARD;

				}
			}
		}

		this.measureUnitService.addMeasureUnit(measureunit);
		FORWARD = "success";
		return FORWARD;

	}

	public String addInput() throws Throwable {
		FORWARD = "success";
		return FORWARD;
	}

	public String deleteMeasure() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			PBaseMeasureunit measure = this.measureUnitService
					.getMeasureUnitById(Integer.valueOf((String) rowlist[i]));

			if (measure.getIsdefault() == 1) {

				setSuccessString("默认的计量单位不能删除");
				FORWARD = "input";
				return FORWARD;
			}
		}

		measureUnitService.deleteMeasureUnit(rowlist);
		FORWARD = "success";
		return FORWARD;

	}

	public String showEditMeasure() throws Throwable {
		measureunit = this.measureUnitService.getMeasureUnitById(Integer
				.valueOf((String) rowlist[0]));
		int measureGroupId = measureunit.getMeasureGroupId();
		measureunitgroup = measureGroupService
				.getMeasureGroupById(measureGroupId);
		FORWARD = "success";
		return FORWARD;
	}

	public String showMeasureList() throws Throwable {
		measureDTOList = this.measureUnitService.getMeasureUnitAll();
		FORWARD = "success";
		return FORWARD;

	}

	public String selectMeasure() throws Throwable {
		rowIndex = ServletActionContext.getRequest().getParameter("rowIndex");
		if (measureunit == null) {
			measureDTOList = measureUnitService.getMeasureUnitAll();
		} else {
			if (!measureunit.getMeasureUnitCode().trim().equals("")
					&& !measureunit.getMeasureUnitName().trim().equals("")) {
				measureDTOList = measureUnitService
						.getMeasureUnitByNameAndCode(measureunit
								.getMeasureUnitName().trim(), measureunit
								.getMeasureUnitCode().trim());
			} else if (!measureunit.getMeasureUnitCode().trim().equals("")) {
				measureDTOList = measureUnitService
						.getMeasureUnitByCode(measureunit.getMeasureUnitCode()
								.trim());
			} else if (!measureunit.getMeasureUnitName().trim().equals("")) {
				measureDTOList = measureUnitService
						.getMeasureUnitByName(measureunit.getMeasureUnitName()
								.trim());
			} else {
				measureDTOList = measureUnitService.getMeasureUnitAll();

			}
		}
		measureunit = null;
		FORWARD = "success";
		return FORWARD;
	}

	public String editMeasure() throws Throwable {

		if (this.measureUnitService.getMeasureUnitById(
				measureunit.getMeasureUnitId()).getIsdefault() == 1
				&& measureunit.getIsdefault() == 1) {
			this.measureUnitService.updateMeasureUnit(measureunit);
			FORWARD = "success";
			return FORWARD;

		} else {

			measureDTOList = this.measureUnitService.getMeasureUnitAll();
			if (measureunit.getIsdefault() == 1) {
				for (int i = 0; i < measureDTOList.size(); i++) {

					if (measureDTOList.get(i).getMeasureunit()
							.getMeasureGroupId() == measureunit
							.getMeasureGroupId()
							&& measureDTOList.get(i).getMeasureunit()
									.getIsdefault() == 1
							&& measureunit.getIsdefault() == 1) {
						setSuccessString("同一个计量单位组中只能有一个默认单位");
						FORWARD = "input";
						return FORWARD;

					}
				}
			}

			this.measureUnitService.updateMeasureUnit(measureunit);
			FORWARD = "success";
			return FORWARD;
		}
	}

	public String searchMeasure() throws Throwable {
		if (measureunit == null) {
			measureDTOList = this.measureUnitService.getMeasureUnitAll();
		} else {

			if (!measureunit.getMeasureUnitCode().trim().equals("")
					&& !measureunit.getMeasureUnitName().trim().equals("")) {
				measureDTOList = this.measureUnitService
						.getMeasureUnitByNameAndCode(measureunit
								.getMeasureUnitName().trim(), measureunit
								.getMeasureUnitCode().trim());
			} else if (!measureunit.getMeasureUnitCode().trim().equals("")) {

				measureDTOList = this.measureUnitService
						.getMeasureUnitByCode(measureunit.getMeasureUnitCode()
								.trim());
			} else if (!measureunit.getMeasureUnitName().trim().equals("")) {
				measureDTOList = this.measureUnitService
						.getMeasureUnitByName(measureunit.getMeasureUnitName()
								.trim());
			} else {
				measureDTOList = this.measureUnitService.getMeasureUnitAll();

			}
		}
		measureunit = null;
		FORWARD = "success";
		return FORWARD;
	}

	public String selectByGroupId() throws Throwable {
		HttpServletRequest request = ServletActionContext.getRequest();
		String measureGroupId = (String) request.getParameter("measureGroupId");

		PBaseMeasureunit measuer = new PBaseMeasureunit();

		measuer.setMeasureGroupId(Integer.parseInt(measureGroupId));
		measureDTOList = measureUnitService.getMeasureUnitByGroupId(measuer);
		measureunit = null;
		FORWARD = "success";
		return FORWARD;
	}

	public String getFORWARD() {
		return FORWARD;
	}

	public void setFORWARD(String forward) {
		FORWARD = forward;
	}

	public String getSuccessString() {
		return successString;
	}

	public void setSuccessString(String successString) {
		this.successString = successString;
	}

	public String getCost() throws Throwable {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setContentType("text/html");

		response.setCharacterEncoding("UTF-8");
		String measureUnitName = request.getParameter("measureUnitName");
		double basicCost = Double.parseDouble(request.getParameter("basicCost")
				.equals("") ? "1" : request.getParameter("basicCost"));
		PBaseMeasureunit unit = measureUnitService
				.getMeasureUnitByUnitName(measureUnitName);

		// warn:
		// pls note this formula
		// cost*rate=defaultRate*BasicCost
		int rate = unit.getRates();
		int defaultRate = measureUnitService.getDefaultMeasureUnitByGroupId(
				unit.getMeasureGroupId()).getRates();

		String cost = MathUtil.formatDouble((double) defaultRate * basicCost
				/ rate, TWO);

		response.getWriter().write(rowIndex);
		response.getWriter().write("," + cost);
		// response.getWriter().write("," + "12.00");

		return null;
	}

	public String getBasicCost() throws Throwable {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setContentType("text/html");

		response.setCharacterEncoding("UTF-8");
		String measureUnitName = request.getParameter("measureUnitName");
		double cost = Double.parseDouble(request.getParameter("cost")
				.equals("") ? "1" : request.getParameter("cost"));
		PBaseMeasureunit unit = measureUnitService
				.getMeasureUnitByUnitName(measureUnitName);

		// warn:
		// pls note this formula
		// cost*rate=defaultRate*basicCost
		int rate = unit.getRates();
		int defaultRate = measureUnitService.getDefaultMeasureUnitByGroupId(
				unit.getMeasureGroupId()).getRates();
		String basicCost = MathUtil.formatDouble((double) rate * cost
				/ defaultRate, TWO);
		response.getWriter().write(rowIndex);
		response.getWriter().write("," + rate*cost);
		// response.getWriter().write("," + "12.00");

		return null;
	}
	/***
	 * sellPriceDetail-单位数量换算(数量（到）到基本数量（到）的转换)
	 * @return
	 * @throws Throwable
	 */
	public String getBaseUnitNoEnd() throws Throwable {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
	
		String measureUnitName = request.getParameter("measureUnitName");
		double amountEnd = Double.parseDouble(request.getParameter("amountEnd")
				.equals("") ? "1" : request.getParameter("amountEnd"));
		PBaseMeasureunit unit = measureUnitService.getMeasureUnitForSellPrice(measureUnitName);

		int rate = unit.getRates();
		int defaultRate = measureUnitService.getDefaultMeasureUnitByGroupId(
				unit.getMeasureGroupId()).getRates();
		String baseUnitNoEnd = MathUtil.formatDouble((double) rate * amountEnd
				/ defaultRate, TWO);
		response.getWriter().write(rowIndex);
		response.getWriter().write("," + rate*amountEnd);

		return null;
	}
	/***
	 * sellPriceDetail-单位数量换算(数量（从）到基本数量（从）的转换)
	 * @return
	 * @throws Throwable
	 */
	public String getBaseUnitNoStart() throws Throwable {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
	
		String measureUnitName = request.getParameter("measureUnitName");
		double amountStart = Double.parseDouble(request.getParameter("amountStart")
				.equals("") ? "1" : request.getParameter("amountStart"));
		PBaseMeasureunit unit = measureUnitService.getMeasureUnitForSellPrice(measureUnitName);

		int rate = unit.getRates();
		int defaultRate = measureUnitService.getDefaultMeasureUnitByGroupId(
				unit.getMeasureGroupId()).getRates();
		String baseUnitNoStart = MathUtil.formatDouble((double) rate * amountStart
				/ defaultRate, TWO);
		response.getWriter().write(rowIndex);
		response.getWriter().write("," + rate*amountStart);

		return null;
	}
}
